using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class AgentDAL
    {
        public static int Save(AgentInfo info)
        {
            string sql = string.Format("insert into [agent]([AdminID],[AgentName],[Mobile],[WeChat],[QRCode],[CreateDate]) values({0},'{1}','{2}','{3}','{4}','{5}');select @@identity;",info.AdminID,info.AgentName,info.Mobile,info.WeChat,info.QRCode,info.CreateDate);            
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));            
        }
        public static int Update(AgentInfo info)
        {
            string sql = string.Format("update [agent] set [AdminID]={0},[AgentName]='{1}',[Mobile]='{2}',[WeChat]='{3}',[QRCode]='{4}',[CreateDate]='{5}' where [ID]={6}",info.AdminID,info.AgentName,info.Mobile,info.WeChat,info.QRCode,info.CreateDate,info.ID);            
            return CommonDAL.ExecuteNoneQuery(sql) ? info.ID : 0;
        }
        public static AgentInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[agent]");
            return SimpleData(dr);
        }
        public static List<AgentInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[agent]");
            return ListSimpleData(dt);
        }        
        public static List<AgentInfo> FindPage(string keyword, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = "";
            if (!string.IsNullOrEmpty(keyword))
            {
                filter += " and [agent].AgentName like '%" + keyword + "%'";
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[agent]", out count);
            List<AgentInfo> list = new List<AgentInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }

        public static List<AgentInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [agent]"));
            return ListSimpleData(dt);
        }
        public static AgentInfo FindOneByAdminID(int id)
        {
            DataRow dr = CommonDAL.GetDataRow("select * from [agent] where AdminID="+id);
            return SimpleData(dr);
        }
        public static AgentInfo FindOneByActiveID(int id)
        {
            DataRow dr = CommonDAL.GetDataRow("select a.* from [agent] a left join [user] b on a.AdminID = b.AdminID  left join [active] c on c.UserID = b.ID where c.ID =" + id);
            return SimpleData(dr);
        }
        private static AgentInfo SimpleData(DataRow dr)
        {
            AgentInfo info = new AgentInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.AgentName = DataConvert.ToString(dr["AgentName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.WeChat = DataConvert.ToString(dr["WeChat"]);
                info.QRCode = DataConvert.ToString(dr["QRCode"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<AgentInfo> ListSimpleData(DataTable dt)
        {
            List<AgentInfo> list = new List<AgentInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
    }
}

